<template>
  <div class="bottom-bar">
    <div class="check-content" @click="selectAll">
      <check-button class="check-button" :value="isSelectAll" />
      <span>全选</span>
    </div>

    <div class="price">
      合计: {{totalPrice}}
    </div>

    <div class="calculate" @click="calculate">
      去结算({{checkedLength}})
    </div>
  </div>
</template>

<script>
import CheckButton from 'components/context/CheckButton'

export default {
  name: "CarBotttomBar",
  components: {
    CheckButton
  },
  methods: {
    selectAll() {
      let checked = !this.isSelectAll;
      this.$store.getters.cartList.forEach(p => p.checked = checked);
    },
    calculate() {
      if (!this.checkedLength) {
        this.$toast.show('请选择商品进行结算');
      }
    }
  },
  computed: {
    totalPrice() {
      return this.$store.getters.cartList.filter(p => p.checked)
        .reduce((prev, cur) => {
          return prev + cur.price * cur.count;
        }, 0).toFixed(2);
    },
    checkedLength() {
      return this.$store.getters.cartList.filter(p => p.checked).length;
    },
    isSelectAll() {
      const cartList = this.$store.getters.cartList;
      // 使用filter函数
      // return !(cartList.filter(p => !p.checked).length);

      // 使用find函数
      return cartList.length > 0 && !cartList.find(p => !p.checked);
    }
  }
}
</script>

<style scoped>
.bottom-bar {
  position: relative;
  display: flex;

  height: 40px;
  line-height: 40px;

  background-color: #eee;

  font-size: 14px;
}

.check-content {
  display: flex;
  align-items: center;
  margin-left: 10px;
  width: 60px;
}

.check-button {
  width: 20px;
  height: 20px;
  line-height: 20px;
}

.price {
  margin-left: 30px;
  flex: 1;
}

.calculate {
  width: 90px;
  margin-right: 5px;

  background-color: red;
  color: #fff;
  text-align: center;
}
</style>

